package com.youdo.careyes.ui;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.math.MathUtils;

import com.bumptech.glide.Glide;
import com.youdo.careyes.R;
import com.youdo.careyes.model.AppItemViewHolder;
import com.youdo.careyes.utils.VideoFileSearcher;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 文件管理 适配器
 * <p>
 * 支持分页;by ljb on 2025.11.28
 */
public class FileItemAdapter extends BaseAdapter {
    private Context context;
    private TextView tvNum;//显示页码
    private VideoFileSearcher.VideoInfo currentSelectedApp;
    private int height;
    private final List<VideoFileSearcher.VideoInfo> ramList = new ArrayList<>();// 原始数据
    private final List<VideoFileSearcher.VideoInfo> curList = new ArrayList<>();// 当前显示的数据
    private int curPage = 0;//当前的页面
    private int totalPage = 0;//当前的页面

    public FileItemAdapter(Context context, TextView tvNum) {
        this.context = context;
        this.tvNum = tvNum;
    }

    @Override
    public int getCount() {
        return curList.size();
    }

    @Override
    public VideoFileSearcher.VideoInfo getItem(int i) {
        return curList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        AppItemViewHolder viewHolder = null;
        if (null == view) {
            viewHolder = new AppItemViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.file_ltem_view, null);
            viewHolder.app_icon = (ImageView) view.findViewById(R.id.app_item_icon);
            viewHolder.app_name = (TextView) view.findViewById(R.id.app_name);
            view.setTag(viewHolder);
        } else {
            viewHolder = (AppItemViewHolder) view.getTag();
        }
        final VideoFileSearcher.VideoInfo myAppInfo = (VideoFileSearcher.VideoInfo) this.curList.get(position);
        if (myAppInfo != null) {
            viewHolder.app_name.setText(myAppInfo.name);
            if (myAppInfo.isImage()) {
                Glide.with(context)
                        .load(myAppInfo.path)
                        .thumbnail(0.1f) // 先加载缩略图
                        .into(viewHolder.app_icon);
            } else {
                if (null != myAppInfo.img) {
                    viewHolder.app_icon.setImageBitmap(myAppInfo.img);
                } else {
                    viewHolder.app_icon.setImageResource(R.mipmap.icon_file_video);
                }
            }

        }
        return view;
    }

    public void notifyDataChange(List<VideoFileSearcher.VideoInfo> list) {
        this.ramList.clear();
        this.ramList.addAll(list);
        initData();
    }

    public void addListItem(VideoFileSearcher.VideoInfo info) {
        this.ramList.add(info);
    }

    public VideoFileSearcher.VideoInfo getItemAppInfo(int position) {
        return (VideoFileSearcher.VideoInfo) this.ramList.get(position);
    }

    public VideoFileSearcher.VideoInfo getCurrentSelectedApp() {
        return this.currentSelectedApp;
    }

    public List<VideoFileSearcher.VideoInfo> getRamList() {
        return this.ramList;
    }

    public void removeItem(VideoFileSearcher.VideoInfo myAppInfo) {
        this.ramList.remove(myAppInfo);
        notifyDataSetChanged();
    }

    public void next() {
        curPage++;
        initData();
    }

    public void previous() {
        curPage--;
        initData();
    }

    private void initData() {
        totalPage = (int) Math.ceil((double) ramList.size() / 12);// 从1开始
        if (totalPage > 0) {
            curPage = MathUtils.clamp(curPage, 1, totalPage);// 从1开始
        } else {
            totalPage = 1;
            curPage = 1;
        }
        this.tvNum.setText(String.format(Locale.US, "(%d/%d)", curPage, totalPage));
        int start = (curPage - 1) * 12;
        int end = Math.min((start + 12), ramList.size());

        this.curList.clear();
        for (int i = start; i < end; i++) {
            this.curList.add(ramList.get(i));
        }
        Log.e("FileItemAdapter", "更新显示22:curPage=" + curPage + ",totalPage=" + totalPage + ",ram size=" + ramList.size() + ",start=" + start + ",end=" + end + ",cur size=" + curList.size());
        notifyDataSetChanged();
    }

}
